package com.simonall.gui;

import com.simonall.enums.ResponseCodeEnum;
import com.simonall.gui.action.HomeAction;
import com.simonall.gui.conf.Img;
import com.simonall.gui.utils.CustomTray;
import com.simonall.gui.utils.CustomXmlLoader;
import com.simonall.utils.BusinessException;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * 桌面
 * 
 * @author simonall
 */
public class Gui extends Application {

	private static Gui instance;

	public static Gui getInstance() {
		return instance;
	}

	// 坐标
	private double x = 0;
	private double y = 0;

	private Stage stage;

	@Override
	public void init() throws Exception {
		instance = this;
	}

	@Override
	public void start(Stage stage) throws Exception {
		CustomTray.getInstance().listen(this.stage = stage);
		stage.initStyle(StageStyle.DECORATED);
		stage.getIcons().add(new Image(Img.ICON));
		stage.setTitle(ResponseCodeEnum.APP_NAME.getDesc());
		Gui.getInstance().display(CustomXmlLoader.load(HomeAction.class));
	}

	/**
	 * 显示窗口
	 * 
	 * @param node 节点
	 * @throws BusinessException
	 */
	public void display(Parent node) throws BusinessException {
		node.setOnMousePressed(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				x = event.getSceneX();
				y = event.getSceneY();
			}
		});
		node.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				stage.setX(event.getScreenX() - x);
				stage.setY(event.getScreenY() - y);
			}
		});
		Scene scene = new Scene(node);
		stage.setScene(scene);
		stage.show();
	}

}
